﻿using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Poet.Commands;

namespace Poet.ViewModels
{
    /// <summary>
    /// Search And Replace View Model.
    /// </summary>
    internal class SearchAndReplaceViewModel : SearchViewModel
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SearchAndReplaceViewModel"/> class.
        /// </summary>
        /// <param name="mainViewModel">The main view model.</param>
        public SearchAndReplaceViewModel(MainViewModel mainViewModel)
            : base(mainViewModel)
        {
            Replace = new RoutedUICommand();
            CommandBindings.Add(new CommandBinding(Replace, ReplaceExecuted, ReplaceCanExecute));
        }

        /// <summary>
        /// Gets or sets the replace.
        /// </summary>
        /// <value>
        /// The replace.
        /// </value>
        public ICommand Replace { get; protected set; }

        /// <summary>
        /// Gets or sets the replace pattern.
        /// </summary>
        /// <value>
        /// The replace pattern.
        /// </value>
        public string ReplacePattern
        {
            get { return Get(() => ReplacePattern); }
            set { Set(() => ReplacePattern, value); }
        }

        /// <summary>
        /// Gets or sets the recent regex patterns.
        /// </summary>
        /// <value>
        /// The recent regex patterns.
        /// </value>
        public ObservableCollection<string> RecentReplacePatterns { get; set; }

        private void ReplaceCanExecute(object sender, CanExecuteRoutedEventArgs args)
        {
            if (ActiveDocumentView == null)
                return;

            var directSearch = bool.Parse((string)args.Parameter);
            if (!LookInViewModel.SelectedItems.Contains(ActiveDocumentView))
                LookInViewModel.SelectedItems.Add(ActiveDocumentView);
            var items = LookInViewModel.Items.Where(i => LookInViewModel.SelectedItems.Contains(i)).ToList();
            var command = directSearch
                              ? SearchAndReplaceCommands.ReplaceNext
                              : SearchAndReplaceCommands.ReplacePrev;
            args.CanExecute = CanExecuteOrExecute(ActiveDocumentView, command, items, directSearch, false);
        }

        private void ReplaceExecuted(object sender, ExecutedRoutedEventArgs args)
        {
            if (ActiveDocumentView == null)
                return;

            var directSearch = bool.Parse((string)args.Parameter);
            if (!LookInViewModel.SelectedItems.Contains(ActiveDocumentView))
                LookInViewModel.SelectedItems.Add(ActiveDocumentView);
            var items = LookInViewModel.Items.Where(i => LookInViewModel.SelectedItems.Contains(i)).ToList();
            var command = directSearch
                              ? SearchAndReplaceCommands.ReplaceNext
                              : SearchAndReplaceCommands.ReplacePrev;
            CanExecuteOrExecute(ActiveDocumentView, command, items, directSearch, true);
        }
    }
}
